<?PHP
if (is_array($entity)) {
    foreach ($entity as $entity_item) {
        ?>
        <input name="ajax_id" id="ajax_id" type="hidden" value="<?PHP echo $entity_item['se_id']; ?>"/>
        <?php
    }
}
?>
<link href="<?php echo base_url(); ?>DataTable/demo_page.css" rel="stylesheet" type="text/css" />
<link href="<?php echo base_url(); ?>DataTable/demo_table.css" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url(); ?>script/DataTable/jquery.dataTables.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        oTable=$('#ListTable').dataTable({
            "bPaginate":true,
            "bLengthChange": false, 
            "bFilter":false,
            "bInfo":false,
            "oLanguage": {//语言国际化
                "sUrl": "script/DataTable/zh.text"
            }
        });
        //初始化Grid数据
        $.ajax({
            url: "<?php echo site_url(); ?>/basic/C_Basic_Series_Edit/ajax_GetProduct/"+$("#ajax_id").val(),
            cache:false,
            success:function(result){
                if(result!=''){
                    var dataObj=eval("("+result+")");;
                    $.each(dataObj, function(i,item) {
                        //填入grid
                        $('#ListTable').dataTable().fnAddData( [
                            item.pt_id,
                            item.pt_name,
                            item.pt_num,
                            "<a href='javascript:void(0);' onclick='deleteTable(this)'>删除</a>" ]
                    );
                    })
                }
            }
        });
    });
</script>
<div class="right01"><img src="content/images/04.gif" /> <a href="<?php echo site_url(); ?>/C_Main/index/">主页</a> &gt; <span>套系信息配置</span>  &gt; <span>操作套系信息</span> 
</div>
<div id="content">
    <div id="tabsCount"> 
        <?php
        $attributes = array('id' => 'submitform', 'method' => "POST", 'accept-charset' => "utf-8");
        echo form_open('basic/C_Basic_Series_Edit/edit_Product', $attributes);
        ?>
        <table class="tables">
            <?PHP
            if (is_array($entity)) {
                foreach ($entity as $entity_item) {
                    ?>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style="width:12%">套系名称：</td>
                        <td style="width:19%"><input id="se_name" name="se_name" type="text" value="<?PHP echo $entity_item['se_name']; ?>"/><span style=" color: red">*</span></td>
                        <td style="width:12%">套系金额：</td>
                        <td style="width:19%"><input id="se_moeny" name="se_moeny" type="text" value="<?PHP echo $entity_item['se_moeny']; ?>"/><span style=" color: red">*</span></td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">&nbsp;</td>
                        <td style=" width: 19%;">&nbsp;</td>
                        <td style=" width: 12%;">&nbsp;</td>
                        <td style="width:19%;text-align:right">
                            <span style="cursor:pointer" id="add_button">[添加]</span> &nbsp; &nbsp;
                        </td>
                        <td style=" width: 19%;">&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan="6">
                            <table id="ListTable" class="tables">
                                <thead>
                                    <tr>
                                        <th>
                                            商品编号
                                        </th>
                                        <th>
                                            商品名称
                                        </th>
                                        <th>
                                            数量
                                        </th>
                                        <th>
                                            操作
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>

                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <input name="se_id" id="se_id" type="hidden" value="<?PHP echo $entity_item['se_id']; ?>"/>
                    <input name="se_item" id="se_item" type="hidden"/>
                    <?PHP
                }
            }
            ?>
        </table>
        <div style="margin-top: 20%; margin-left: 35%;">
            <input class="button" type="submit"  name="postsubmit" value="提交"/>
            <input class="button" type="button" name="resetbtn" onClick="resetButton()" value="返回"/>
        </div>
        </form>
    </div>

    <div id="show_dialog" title="选择商品" style="display:none;">
        <table width='100%'>
            <tr>
                <td style="width:20%">商品名称</td>
                <td style="width:30%">
                    <select name='product_name' id='product_name'>
                        <?PHP
                        if (is_array($productlist)) {
                            foreach ($productlist as $product_item) {
                                ?>
                                <option value ="<?PHP echo $product_item['pt_id']; ?>"><?PHP echo $product_item['pt_name']; ?></option>
                                <?PHP
                            }
                        }
                        ?>
                    </select>
                </td>
                <td style="width:20%">数量</td>
                <td style="width:30%">
                    <input id="product_number" name="product_number" value="1"  type="text" value="" style="width:100px;"/>
                </td>
            </tr>
        </table>
    </div>
</div>
<script type="text/javascript">
    //提交插入信息时，获取表格全部信息
    $("#submitform").submit(function(){
        //判断信息的完整性
        if ($("#se_name").val()=="" || $("#se_moeny").val()=="") {
            $.prompt("<span style='color:#FF0000'>请确认信息填写完整！</span>", {
                title: "提示",
                buttons: { "确定": true},
                timeout:5000
            });
            return false;
        }
        
        var tableTr = $('#ListTable tr');
        var resultStr="";
        var index=0;
        $("td",tableTr).each(function(){
            if($(this).text()!="没有检索到数据"){
                if($(this).text()!="删除"){
                    resultStr+=$(this).text()+",";
                }else{
                    resultStr+=";";
                }
                index++;
            }
        });
        if (index==0) {
            $.prompt("<span style='color:#FF0000'>亲，请选择需要的商品哦！</span>", {
                title: "提示",
                buttons: { "确定": true},
                timeout:5000
            });
            return false;
        }
        $("#se_item").val(resultStr);
    });
    
    //DIV添加山出框
    var statesdemo = {
        state0: {
            title: '选择商品',
            html:$("#show_dialog").html(),
            buttons: { 确定: 1 },
            focus: 1,
            submit:function(e,v,m,f){ 
                if (f.product_number == "") {
                    $("#product_number").css("border","1px solid red");
                    return false;
                }
                //填入grid
                $('#ListTable').dataTable().fnAddData( [
                    f.product_name,
                    m.find("option:selected").text(),
                    f.product_number,
                    "<a href='javascript:void(0);' onclick='deleteTable(this)'>删除</a>" ]
            );
                //清空文本内容
                $("#show_dialog input[type=text]").val("");
            }
        }
    };
    $("#add_button").click(function(){
        $.prompt(statesdemo);
    })
    //返回
    function resetButton(){
        window.location.href="<?php echo site_url(); ?>/basic/C_Basic_Series_List/";
    }
    
    //删除table中的一行
    function deleteTable(vals){
        var row = $(vals).closest("tr").get(0);
        oTable.fnDeleteRow(oTable.fnGetPosition(row));
    }
</script>